﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq;
using System.Text;
using Tesla.Service.DataContracts;
using Tesla.DataAccess.DataAccessComponents;
using Tesla.Util.Utilities;
using Tesla.DataAccess.DataAccessLogic.Interfaces;

namespace Tesla.DataAccess.DataAccessLogic.Implementations
{
    public class ProjectDA : BaseOperationalDA
    {
        #region BaseOperationalDA Members
        public override int? Create(BaseDC bDC)
        {
            int success = 0;

            try
            {
                using (TeslaDataContext context = new TeslaDataContext())
                {
                    context.DeferredLoadingEnabled = false;

                    success = context.usp_CreateProject(
                                                SetSafeString(((ProjectDC)bDC).ProjectCode),
                                                SetSafeInt(((ProjectDC)bDC).ProjectBillingType),
                                                SetSafeInt(((ProjectDC)bDC).ProjectType),
                                                SetSafeDateTime(((ProjectDC)bDC).ProjectPlannedStartDate),
                                                SetSafeDateTime(((ProjectDC)bDC).ProjectPlannedEndDate));


                    context.SubmitChanges();
                    return success;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public override int Update(BaseDC bDC)
        {
            int recordCount = 0;

            try
            {
                using (TeslaDataContext context = new TeslaDataContext())
                {
                    context.DeferredLoadingEnabled = false;
                    recordCount = context.usp_UpdateProject(
                                                SetSafeInt(((ProjectDC)bDC).ProjectID),
                                                SetSafeString(((ProjectDC)bDC).ProjectCode),
                                                SetSafeInt(((ProjectDC)bDC).ProjectBillingType),
                                                SetSafeInt(((ProjectDC)bDC).ProjectType),
                                                SetSafeDateTime(((ProjectDC)bDC).ProjectPlannedStartDate),
                                                SetSafeDateTime(((ProjectDC)bDC).ProjectPlannedEndDate));
                    context.SubmitChanges();
                    return recordCount;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public override IList<BaseDC> DisplayAll(int gridSize, ref int? totalRecordCount)
        {
            IList<BaseDC> projects = null;
            try
            {

                using (TeslaDataContext context = new TeslaDataContext())
                {
                    context.DeferredLoadingEnabled = false;
                    IEnumerable<usp_DisplayAllProjectResult> projectList = context.usp_DisplayAllProject(gridSize, ref totalRecordCount).Cast<usp_DisplayAllProjectResult>();
                    projects = GenericConversion.ConvertToTargetType(projectList.ToList<usp_DisplayAllProjectResult>().ToArray(), typeof(usp_DisplayAllProjectResult), typeof(ProjectDC));

                }

                return projects;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public override IList<BaseDC> SearchByName(string name, int gridSize, ref int? totalRecordCount)
        {
            IList<BaseDC> projects = null;

            try
            {

                using (TeslaDataContext context = new TeslaDataContext())
                {
                    context.DeferredLoadingEnabled = false;
                    IEnumerable<usp_SearchProjectByNameResult> projectList = context.usp_SearchProjectByName(name, gridSize, ref totalRecordCount).Cast<usp_SearchProjectByNameResult>();
                    projects = GenericConversion.ConvertToTargetType(projectList.ToList<usp_SearchProjectByNameResult>().ToArray(), typeof(usp_SearchProjectByNameResult), typeof(ProjectDC));
                }
                return projects;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public override IList<BaseDC> Read(int id)
        {
            try
            {
                using (TeslaDataContext context = new TeslaDataContext())
                {
                    context.DeferredLoadingEnabled = false;
                    IEnumerable<usp_ReadProjectResult> projectList = context.usp_ReadProject(id).Cast<usp_ReadProjectResult>();
                    return GenericConversion.ConvertToTargetType(projectList.ToList<usp_ReadProjectResult>().ToArray(), typeof(usp_ReadProjectResult), typeof(ProjectDC));                    
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public override IList<BaseDC> ReadByRange(int startRecordNo, int endRecordNo, string sortColName, string sortDirection, string searchString)
        {
            try
            {
                using (TeslaDataContext context = new TeslaDataContext())
                {
                    context.DeferredLoadingEnabled = false;
                    IEnumerable<usp_ReadProjectByRowNumberRangeResult> projectList = context.usp_ReadProjectByRowNumberRange(startRecordNo, endRecordNo, sortColName, sortDirection, searchString).Cast<usp_ReadProjectByRowNumberRangeResult>();
                    return GenericConversion.ConvertToTargetType(projectList.ToList<usp_ReadProjectByRowNumberRangeResult>().ToArray(), typeof(usp_ReadProjectByRowNumberRangeResult), typeof(ProjectDC));
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion
    }
}
